from selenium import webdriver
from time import sleep


from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
#多元素定位和处理
driver = webdriver.Chrome()
sleep(2)
driver.get("http://www.baidu.com")
sleep(2)
driver.maximize_window()
sleep(2)
# find_elements返回值是一个列表
# elements=driver.find_elements(By.TAG_NAME,"input")
# print(type(elements))
# print(elements)
# for e in elements:
#     if e.get_attribute("id")=="kw" :
#         e.send_keys("hello")
#     # print(e.get_attribute("type"))
#     # print(e.get_attribute("name"))
# sleep(2)
element = driver.find_element(By.CSS_SELECTOR,"#kw")
#多个连续操作
ActionChains(driver).send_keys_to_element(element,"hello").click(element).perform()
# sleep(2)
# # 全选
# ActionChains(driver).double_click(element).perform()
# sleep(2)
# #右键
# ActionChains(driver).context_click(element).perform()
# sleep(2)
# element = driver.find_element(By.XPATH,'//*[@id="u"]/a[2]')
# 将鼠标移动到某个元素
# ActionChains(driver).move_to_element(element).perform()
# sleep(2)
from selenium.webdriver import Keys
element = driver.find_element(By.CSS_SELECTOR,"#kw")
# 全选
element.send_keys(Keys.CONTROL,'a')
sleep(2)
# 复制
element.send_keys(Keys.CONTROL,'c')
sleep(2)
# 删除
element.send_keys(Keys.BACKSPACE)
sleep(2)
# 粘贴
element.send_keys(Keys.CONTROL,'v')
sleep(2)
driver.quit()